#!/usr/bin/python2
# coding=utf-8

"Random and interval utilities"

"""
    Ball Fall - a 2d arcade game written in Python
    Copyright (C) 2008 Bartosz Gąsiorzewski <g.bartek@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from random import randint

def every(how_many):
    # generator, accepts frequency (how_many) as an argument
    # returns True or False, depending on given frequency
    # e.g. every(1/2.) alternates False and True,
    # every(2/5.) returns F, F, T, F, T over and over again
    # for numbers such as 1/3 may give an approximation
    count = 0.0
    how_many = float(how_many)
    while True:
        count += how_many
        yield count >= 1
        count %= 1

def myRand(min, max):
    # returns True or False
    # with every returned F, chance of returning T increases
    # XXX: algorithm sucks, for big min returns T too soon.
    chance = 1
    while True:
        if chance > randint(min, max):
            chance = 1
            yield True
        else:
            chance += 1
            yield False
